package com.koushikdutta.async;

import android.os.Build.VERSION;
import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.callback.DataCallback;
import com.koushikdutta.async.callback.WritableCallback;
import com.koushikdutta.async.util.Allocator;
import com.koushikdutta.async.wrapper.AsyncSocketWrapper;
import java.nio.ByteBuffer;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLEngineResult;
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
import javax.net.ssl.SSLEngineResult.Status;
import javax.net.ssl.SSLException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class AsyncSSLSocketWrapper implements AsyncSSLSocket, AsyncSocketWrapper {
    static final /* synthetic */ boolean $assertionsDisabled;
    static SSLContext defaultSSLContext;
    boolean clientMode;
    SSLEngine engine;
    boolean finishedHandshake;
    HandshakeCallback handshakeCallback;
    HostnameVerifier hostnameVerifier;
    DataCallback mDataCallback;
    BufferedDataEmitter mEmitter;
    private String mHost;
    private int mPort;
    BufferedDataSink mSink;
    AsyncSocket mSocket;
    boolean mUnwrapping;
    private boolean mWrapping;
    WritableCallback mWriteableCallback;
    X509Certificate[] peerCertificates;
    TrustManager[] trustManagers;

    public interface HandshakeCallback {
        void onHandshakeCompleted(Exception exception, AsyncSSLSocket asyncSSLSocket);
    }

    static {
        boolean z = true;
        if (AsyncSSLSocketWrapper.class.desiredAssertionStatus()) {
            z = false;
        }
        $assertionsDisabled = z;
        try {
            if (VERSION.SDK_INT <= 15) {
                throw new Exception();
            }
            defaultSSLContext = SSLContext.getInstance("Default");
        } catch (Exception ex) {
            try {
                defaultSSLContext = SSLContext.getInstance("TLS");
                defaultSSLContext.init(null, new TrustManager[]{new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[0];
                    }

                    public void checkClientTrusted(X509Certificate[] certs, String authType) {
                    }

                    public void checkServerTrusted(X509Certificate[] certs, String authType) {
                        for (X509Certificate cert : certs) {
                            if (!(cert == null || cert.getCriticalExtensionOIDs() == null)) {
                                cert.getCriticalExtensionOIDs().remove("2.5.29.15");
                            }
                        }
                    }
                }}, null);
            } catch (Exception ex2) {
                ex.printStackTrace();
                ex2.printStackTrace();
            }
        }
    }

    public static SSLContext getDefaultSSLContext() {
        return defaultSSLContext;
    }

    public static void handshake(AsyncSocket socket, String host, int port, SSLEngine sslEngine, TrustManager[] trustManagers, HostnameVerifier verifier, boolean clientMode, HandshakeCallback callback) {
        AsyncSSLSocketWrapper wrapper = new AsyncSSLSocketWrapper(socket, host, port, sslEngine, trustManagers, verifier, clientMode);
        wrapper.handshakeCallback = callback;
        final HandshakeCallback handshakeCallback = callback;
        socket.setClosedCallback(new CompletedCallback() {
            public void onCompleted(Exception ex) {
                handshakeCallback.onHandshakeCompleted(new SSLException(ex), null);
            }
        });
        try {
            wrapper.engine.beginHandshake();
            wrapper.handleHandshakeStatus(wrapper.engine.getHandshakeStatus());
        } catch (SSLException e) {
            wrapper.report(e);
        }
    }

    private AsyncSSLSocketWrapper(AsyncSocket socket, String host, int port, SSLEngine sslEngine, TrustManager[] trustManagers, HostnameVerifier verifier, boolean clientMode) {
        this.mSocket = socket;
        this.hostnameVerifier = verifier;
        this.clientMode = clientMode;
        this.trustManagers = trustManagers;
        this.engine = sslEngine;
        this.mHost = host;
        this.mPort = port;
        this.engine.setUseClientMode(clientMode);
        this.mSink = new BufferedDataSink(socket);
        this.mSink.setWriteableCallback(new WritableCallback() {
            public void onWriteable() {
                if (AsyncSSLSocketWrapper.this.mWriteableCallback != null) {
                    AsyncSSLSocketWrapper.this.mWriteableCallback.onWriteable();
                }
            }
        });
        this.mEmitter = new BufferedDataEmitter(socket);
        final Allocator allocator = new Allocator();
        allocator.setMinAlloc(8192);
        final ByteBufferList transformed = new ByteBufferList();
        this.mEmitter.setDataCallback(new DataCallback() {
            /* JADX WARNING: inconsistent code. */
            /* Code decompiled incorrectly, please refer to instructions dump. */
            public void onDataAvailable(com.koushikdutta.async.DataEmitter r13, com.koushikdutta.async.ByteBufferList r14) {
                /*
                r12 = this;
                r11 = 1;
                r10 = 0;
                r7 = com.koushikdutta.async.AsyncSSLSocketWrapper.this;
                r7 = r7.mUnwrapping;
                if (r7 == 0) goto L_0x0009;
            L_0x0008:
                return;
            L_0x0009:
                r7 = com.koushikdutta.async.AsyncSSLSocketWrapper.this;	 Catch:{ SSLException -> 0x00b2 }
                r8 = 1;
                r7.mUnwrapping = r8;	 Catch:{ SSLException -> 0x00b2 }
                r7 = r14.hasRemaining();	 Catch:{ SSLException -> 0x00b2 }
                if (r7 == 0) goto L_0x001b;
            L_0x0014:
                r0 = r14.getAll();	 Catch:{ SSLException -> 0x00b2 }
                r14.add(r0);	 Catch:{ SSLException -> 0x00b2 }
            L_0x001b:
                r1 = com.koushikdutta.async.ByteBufferList.EMPTY_BYTEBUFFER;	 Catch:{ SSLException -> 0x00b2 }
            L_0x001d:
                r7 = r1.remaining();	 Catch:{ SSLException -> 0x00b2 }
                if (r7 != 0) goto L_0x002d;
            L_0x0023:
                r7 = r14.size();	 Catch:{ SSLException -> 0x00b2 }
                if (r7 <= 0) goto L_0x002d;
            L_0x0029:
                r1 = r14.remove();	 Catch:{ SSLException -> 0x00b2 }
            L_0x002d:
                r5 = r1.remaining();	 Catch:{ SSLException -> 0x00b2 }
                r7 = r1;	 Catch:{ SSLException -> 0x00b2 }
                r2 = r7.remaining();	 Catch:{ SSLException -> 0x00b2 }
                r7 = r0;	 Catch:{ SSLException -> 0x00b2 }
                r4 = r7.allocate();	 Catch:{ SSLException -> 0x00b2 }
                r7 = com.koushikdutta.async.AsyncSSLSocketWrapper.this;	 Catch:{ SSLException -> 0x00b2 }
                r7 = r7.engine;	 Catch:{ SSLException -> 0x00b2 }
                r6 = r7.unwrap(r1, r4);	 Catch:{ SSLException -> 0x00b2 }
                r7 = com.koushikdutta.async.AsyncSSLSocketWrapper.this;	 Catch:{ SSLException -> 0x00b2 }
                r8 = r1;	 Catch:{ SSLException -> 0x00b2 }
                r7.addToPending(r8, r4);	 Catch:{ SSLException -> 0x00b2 }
                r7 = r0;	 Catch:{ SSLException -> 0x00b2 }
                r8 = r1;	 Catch:{ SSLException -> 0x00b2 }
                r8 = r8.remaining();	 Catch:{ SSLException -> 0x00b2 }
                r8 = r8 - r2;
                r8 = (long) r8;	 Catch:{ SSLException -> 0x00b2 }
                r7.track(r8);	 Catch:{ SSLException -> 0x00b2 }
                r7 = r6.getStatus();	 Catch:{ SSLException -> 0x00b2 }
                r8 = javax.net.ssl.SSLEngineResult.Status.BUFFER_OVERFLOW;	 Catch:{ SSLException -> 0x00b2 }
                if (r7 != r8) goto L_0x0096;
            L_0x0061:
                r7 = r0;	 Catch:{ SSLException -> 0x00b2 }
                r8 = r0;	 Catch:{ SSLException -> 0x00b2 }
                r8 = r8.getMinAlloc();	 Catch:{ SSLException -> 0x00b2 }
                r8 = r8 * 2;
                r7.setMinAlloc(r8);	 Catch:{ SSLException -> 0x00b2 }
                r5 = -1;
            L_0x006f:
                r7 = com.koushikdutta.async.AsyncSSLSocketWrapper.this;	 Catch:{ SSLException -> 0x00b2 }
                r8 = r6.getHandshakeStatus();	 Catch:{ SSLException -> 0x00b2 }
                r7.handleHandshakeStatus(r8);	 Catch:{ SSLException -> 0x00b2 }
                r7 = r1.remaining();	 Catch:{ SSLException -> 0x00b2 }
                if (r7 != r5) goto L_0x001d;
            L_0x007e:
                r7 = r1;	 Catch:{ SSLException -> 0x00b2 }
                r7 = r7.remaining();	 Catch:{ SSLException -> 0x00b2 }
                if (r2 != r7) goto L_0x001d;
            L_0x0086:
                r14.addFirst(r1);	 Catch:{ SSLException -> 0x00b2 }
            L_0x0089:
                r7 = com.koushikdutta.async.AsyncSSLSocketWrapper.this;	 Catch:{ SSLException -> 0x00b2 }
                r8 = r1;	 Catch:{ SSLException -> 0x00b2 }
                com.koushikdutta.async.Util.emitAllData(r7, r8);	 Catch:{ SSLException -> 0x00b2 }
                r7 = com.koushikdutta.async.AsyncSSLSocketWrapper.this;
                r7.mUnwrapping = r10;
                goto L_0x0008;
            L_0x0096:
                r7 = r6.getStatus();	 Catch:{ SSLException -> 0x00b2 }
                r8 = javax.net.ssl.SSLEngineResult.Status.BUFFER_UNDERFLOW;	 Catch:{ SSLException -> 0x00b2 }
                if (r7 != r8) goto L_0x006f;
            L_0x009e:
                r14.addFirst(r1);	 Catch:{ SSLException -> 0x00b2 }
                r7 = r14.size();	 Catch:{ SSLException -> 0x00b2 }
                if (r7 <= r11) goto L_0x0089;
            L_0x00a7:
                r5 = -1;
                r1 = r14.getAll();	 Catch:{ SSLException -> 0x00b2 }
                r14.addFirst(r1);	 Catch:{ SSLException -> 0x00b2 }
                r1 = com.koushikdutta.async.ByteBufferList.EMPTY_BYTEBUFFER;	 Catch:{ SSLException -> 0x00b2 }
                goto L_0x006f;
            L_0x00b2:
                r3 = move-exception;
                r3.printStackTrace();	 Catch:{ all -> 0x00c1 }
                r7 = com.koushikdutta.async.AsyncSSLSocketWrapper.this;	 Catch:{ all -> 0x00c1 }
                r7.report(r3);	 Catch:{ all -> 0x00c1 }
                r7 = com.koushikdutta.async.AsyncSSLSocketWrapper.this;
                r7.mUnwrapping = r10;
                goto L_0x0008;
            L_0x00c1:
                r7 = move-exception;
                r8 = com.koushikdutta.async.AsyncSSLSocketWrapper.this;
                r8.mUnwrapping = r10;
                throw r7;
                */
                throw new UnsupportedOperationException("Method not decompiled: com.koushikdutta.async.AsyncSSLSocketWrapper.4.onDataAvailable(com.koushikdutta.async.DataEmitter, com.koushikdutta.async.ByteBufferList):void");
            }
        });
    }

    void addToPending(ByteBufferList out, ByteBuffer mReadTmp) {
        mReadTmp.flip();
        if (mReadTmp.hasRemaining()) {
            out.add(mReadTmp);
        } else {
            ByteBufferList.reclaim(mReadTmp);
        }
    }

    public void end() {
        this.mSocket.end();
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private void handleHandshakeStatus(javax.net.ssl.SSLEngineResult.HandshakeStatus r19) {
        /*
        r18 = this;
        r14 = javax.net.ssl.SSLEngineResult.HandshakeStatus.NEED_TASK;
        r0 = r19;
        if (r0 != r14) goto L_0x0011;
    L_0x0006:
        r0 = r18;
        r14 = r0.engine;
        r7 = r14.getDelegatedTask();
        r7.run();
    L_0x0011:
        r14 = javax.net.ssl.SSLEngineResult.HandshakeStatus.NEED_WRAP;
        r0 = r19;
        if (r0 != r14) goto L_0x001e;
    L_0x0017:
        r14 = com.koushikdutta.async.ByteBufferList.EMPTY_BYTEBUFFER;
        r0 = r18;
        r0.write(r14);
    L_0x001e:
        r14 = javax.net.ssl.SSLEngineResult.HandshakeStatus.NEED_UNWRAP;
        r0 = r19;
        if (r0 != r14) goto L_0x002b;
    L_0x0024:
        r0 = r18;
        r14 = r0.mEmitter;
        r14.onDataAvailable();
    L_0x002b:
        r0 = r18;
        r14 = r0.finishedHandshake;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        if (r14 != 0) goto L_0x012c;
    L_0x0031:
        r0 = r18;
        r14 = r0.engine;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r14 = r14.getHandshakeStatus();	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r15 = javax.net.ssl.SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        if (r14 == r15) goto L_0x0049;
    L_0x003d:
        r0 = r18;
        r14 = r0.engine;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r14 = r14.getHandshakeStatus();	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r15 = javax.net.ssl.SSLEngineResult.HandshakeStatus.FINISHED;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        if (r14 != r15) goto L_0x012c;
    L_0x0049:
        r0 = r18;
        r14 = r0.clientMode;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        if (r14 == 0) goto L_0x0104;
    L_0x004f:
        r0 = r18;
        r10 = r0.trustManagers;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        if (r10 != 0) goto L_0x0067;
    L_0x0055:
        r14 = javax.net.ssl.TrustManagerFactory.getDefaultAlgorithm();	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r9 = javax.net.ssl.TrustManagerFactory.getInstance(r14);	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r14 = 0;
        r14 = (java.security.KeyStore) r14;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r9.init(r14);	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r10 = r9.getTrustManagers();	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
    L_0x0067:
        r11 = 0;
        r6 = 0;
        r1 = r10;
        r5 = r1.length;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r4 = 0;
    L_0x006c:
        if (r4 >= r5) goto L_0x00c6;
    L_0x006e:
        r8 = r1[r4];	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r0 = r8;
        r0 = (javax.net.ssl.X509TrustManager) r0;	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r13 = r0;
        r0 = r18;
        r14 = r0.engine;	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r14 = r14.getSession();	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r14 = r14.getPeerCertificates();	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r14 = (java.security.cert.X509Certificate[]) r14;	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r14 = (java.security.cert.X509Certificate[]) r14;	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r0 = r18;
        r0.peerCertificates = r14;	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r0 = r18;
        r14 = r0.peerCertificates;	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r15 = "SSL";
        r13.checkServerTrusted(r14, r15);	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r0 = r18;
        r14 = r0.mHost;	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        if (r14 == 0) goto L_0x00c5;
    L_0x0097:
        r0 = r18;
        r14 = r0.hostnameVerifier;	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        if (r14 != 0) goto L_0x00e5;
    L_0x009d:
        r12 = new org.apache.http.conn.ssl.StrictHostnameVerifier;	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r12.<init>();	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r0 = r18;
        r14 = r0.mHost;	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r0 = r18;
        r15 = r0.peerCertificates;	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r16 = 0;
        r15 = r15[r16];	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r15 = org.apache.http.conn.ssl.StrictHostnameVerifier.getCNs(r15);	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r0 = r18;
        r0 = r0.peerCertificates;	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r16 = r0;
        r17 = 0;
        r16 = r16[r17];	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r16 = org.apache.http.conn.ssl.StrictHostnameVerifier.getDNSSubjectAlts(r16);	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r0 = r16;
        r12.verify(r14, r15, r0);	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
    L_0x00c5:
        r11 = 1;
    L_0x00c6:
        r14 = 1;
        r0 = r18;
        r0.finishedHandshake = r14;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        if (r11 != 0) goto L_0x0109;
    L_0x00cd:
        r2 = new com.koushikdutta.async.AsyncSSLException;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r2.<init>(r6);	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r0 = r18;
        r0.report(r2);	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r14 = r2.getIgnore();	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        if (r14 != 0) goto L_0x0109;
    L_0x00dd:
        throw r2;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
    L_0x00de:
        r3 = move-exception;
        r14 = new java.lang.RuntimeException;
        r14.<init>(r3);
        throw r14;
    L_0x00e5:
        r0 = r18;
        r14 = r0.hostnameVerifier;	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r0 = r18;
        r15 = r0.mHost;	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r0 = r18;
        r0 = r0.engine;	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r16 = r0;
        r16 = r16.getSession();	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        r14.verify(r15, r16);	 Catch:{ GeneralSecurityException -> 0x00fb, SSLException -> 0x0101, NoSuchAlgorithmException -> 0x00de, AsyncSSLException -> 0x0134 }
        goto L_0x00c5;
    L_0x00fb:
        r3 = move-exception;
        r6 = r3;
    L_0x00fd:
        r4 = r4 + 1;
        goto L_0x006c;
    L_0x0101:
        r3 = move-exception;
        r6 = r3;
        goto L_0x00fd;
    L_0x0104:
        r14 = 1;
        r0 = r18;
        r0.finishedHandshake = r14;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
    L_0x0109:
        r0 = r18;
        r14 = r0.handshakeCallback;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r15 = 0;
        r0 = r18;
        r14.onHandshakeCompleted(r15, r0);	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r14 = 0;
        r0 = r18;
        r0.handshakeCallback = r14;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r0 = r18;
        r14 = r0.mWriteableCallback;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        if (r14 == 0) goto L_0x0125;
    L_0x011e:
        r0 = r18;
        r14 = r0.mWriteableCallback;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r14.onWriteable();	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
    L_0x0125:
        r0 = r18;
        r14 = r0.mEmitter;	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
        r14.onDataAvailable();	 Catch:{ NoSuchAlgorithmException -> 0x00de, GeneralSecurityException -> 0x012d, AsyncSSLException -> 0x0134 }
    L_0x012c:
        return;
    L_0x012d:
        r3 = move-exception;
        r0 = r18;
        r0.report(r3);
        goto L_0x012c;
    L_0x0134:
        r3 = move-exception;
        r0 = r18;
        r0.report(r3);
        goto L_0x012c;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.koushikdutta.async.AsyncSSLSocketWrapper.handleHandshakeStatus(javax.net.ssl.SSLEngineResult$HandshakeStatus):void");
    }

    private void writeTmp(ByteBuffer mWriteTmp) {
        mWriteTmp.flip();
        if (mWriteTmp.remaining() > 0) {
            this.mSink.write(mWriteTmp);
        }
        if (!$assertionsDisabled && mWriteTmp.hasRemaining()) {
            throw new AssertionError();
        }
    }

    int calculateAlloc(int remaining) {
        int alloc = (remaining * 3) / 2;
        if (alloc == 0) {
            return 8182;
        }
        return alloc;
    }

    public void write(ByteBuffer bb) {
        if (!this.mWrapping && this.mSink.remaining() <= 0) {
            this.mWrapping = true;
            SSLEngineResult res = null;
            ByteBuffer mWriteTmp = ByteBufferList.obtain(calculateAlloc(bb.remaining()));
            do {
                if (this.finishedHandshake && bb.remaining() == 0) {
                    this.mWrapping = false;
                    return;
                }
                int remaining = bb.remaining();
                try {
                    res = this.engine.wrap(bb, mWriteTmp);
                    writeTmp(mWriteTmp);
                    int previousCapacity = mWriteTmp.capacity();
                    ByteBufferList.reclaim(mWriteTmp);
                    mWriteTmp = null;
                    if (res.getStatus() == Status.BUFFER_OVERFLOW) {
                        mWriteTmp = ByteBufferList.obtain(previousCapacity * 2);
                        remaining = -1;
                    } else {
                        mWriteTmp = ByteBufferList.obtain(calculateAlloc(bb.remaining()));
                    }
                    handleHandshakeStatus(res.getHandshakeStatus());
                } catch (SSLException e) {
                    report(e);
                }
                if (remaining != bb.remaining() || (res != null && res.getHandshakeStatus() == HandshakeStatus.NEED_WRAP)) {
                }
            } while (this.mSink.remaining() == 0);
            ByteBufferList.reclaim(mWriteTmp);
            this.mWrapping = false;
        }
    }

    public void write(ByteBufferList bb) {
        if (!this.mWrapping && this.mSink.remaining() <= 0) {
            this.mWrapping = true;
            SSLEngineResult res = null;
            ByteBuffer mWriteTmp = ByteBufferList.obtain(calculateAlloc(bb.remaining()));
            do {
                if (!this.finishedHandshake || bb.remaining() != 0) {
                    int remaining = bb.remaining();
                    try {
                        ByteBuffer[] arr = bb.getAllArray();
                        res = this.engine.wrap(arr, mWriteTmp);
                        bb.addAll(arr);
                        writeTmp(mWriteTmp);
                        int previousCapacity = mWriteTmp.capacity();
                        ByteBufferList.reclaim(mWriteTmp);
                        mWriteTmp = null;
                        if (res.getStatus() == Status.BUFFER_OVERFLOW) {
                            mWriteTmp = ByteBufferList.obtain(previousCapacity * 2);
                            remaining = -1;
                        } else {
                            mWriteTmp = ByteBufferList.obtain(calculateAlloc(bb.remaining()));
                            handleHandshakeStatus(res.getHandshakeStatus());
                        }
                    } catch (SSLException e) {
                        report(e);
                    }
                    if (remaining == bb.remaining() && (res == null || res.getHandshakeStatus() != HandshakeStatus.NEED_WRAP)) {
                        break;
                    }
                } else {
                    break;
                }
            } while (this.mSink.remaining() == 0);
            ByteBufferList.reclaim(mWriteTmp);
            this.mWrapping = false;
        }
    }

    public void setWriteableCallback(WritableCallback handler) {
        this.mWriteableCallback = handler;
    }

    public WritableCallback getWriteableCallback() {
        return this.mWriteableCallback;
    }

    private void report(Exception e) {
        HandshakeCallback hs = this.handshakeCallback;
        if (hs != null) {
            this.handshakeCallback = null;
            this.mSocket.setDataCallback(new NullDataCallback());
            this.mSocket.end();
            this.mSocket.close();
            hs.onHandshakeCompleted(e, null);
            return;
        }
        CompletedCallback cb = getEndCallback();
        if (cb != null) {
            cb.onCompleted(e);
        }
    }

    public void setDataCallback(DataCallback callback) {
        this.mDataCallback = callback;
    }

    public DataCallback getDataCallback() {
        return this.mDataCallback;
    }

    public boolean isOpen() {
        return this.mSocket.isOpen();
    }

    public void close() {
        this.mSocket.close();
    }

    public void setClosedCallback(CompletedCallback handler) {
        this.mSocket.setClosedCallback(handler);
    }

    public void setEndCallback(CompletedCallback callback) {
        this.mSocket.setEndCallback(callback);
    }

    public CompletedCallback getEndCallback() {
        return this.mSocket.getEndCallback();
    }

    public void pause() {
        this.mSocket.pause();
    }

    public void resume() {
        this.mSocket.resume();
    }

    public boolean isPaused() {
        return this.mSocket.isPaused();
    }

    public AsyncServer getServer() {
        return this.mSocket.getServer();
    }

    public AsyncSocket getSocket() {
        return this.mSocket;
    }

    public String charset() {
        return null;
    }
}
